

#import "PickerView.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

@interface PickerView ()  {
    UIDatePicker *_datePick;
    UIView *btnView;
    NSString *dateStr;
    NSDate *dateCurrent;
}

@end

@implementation PickerView


-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self createBtn];
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeView)];
        [self addGestureRecognizer:tap];
        dateStr = @"(null)";
    }
    return self;
}

- (void)removeView {
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self->btnView.frame = CGRectMake(0, self.bounds.size.height, self.bounds.size.width, 40);
        self->_datePick.frame = CGRectMake(0,self.frame.size.height+40, SCREEN_WIDTH, 200);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

//创建 取消 确定按钮
-(void)createBtn {
    NSArray *titleArr = @[@"取消",@"完成"];
    
    
    btnView = [[UIView alloc] init];
    btnView.frame = CGRectMake(0, self.bounds.size.height, self.bounds.size.width, 40);
    btnView.backgroundColor = [UIColor grayColor];
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self->btnView.frame = CGRectMake(0,self.frame.size.height-240, SCREEN_WIDTH, 40);
    } completion:^(BOOL finished) {
        
    }];
    
    [self addSubview:btnView];
    for (int i = 0 ; i < 2; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(((self.bounds.size.width)/2)*i, 0, (self.bounds.size.width)/2, 40);
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        [btn setTitle:titleArr[i] forState:UIControlStateNormal];
        if (i == 0) {
            btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
            [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        } else {
            btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 15);
            [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }
        btn.tag = i+1;
        [btn addTarget:self action:@selector(selectBtn:) forControlEvents:UIControlEventTouchUpInside];
        [btnView addSubview:btn];
    }
    
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-60, 10, 120, 20)];
    title.text = @"选择日期";
    title.textAlignment = 1;
    title.font = [UIFont systemFontOfSize:15];
    title.textColor = [UIColor greenColor];
    [btnView addSubview:title];

}

- (NSString *)reserveDateAfterDays:(long)days {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString * str = [formatter stringFromDate:[[NSDate alloc] initWithTimeInterval:(24 * 60 * 60 * days) sinceDate:[_datePick date]]];
    return str;
}

//按钮回调
-(void)selectBtn:(UIButton *)btn {
    if ([dateStr isEqualToString:@"(null)"]) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd"];
        dateStr = [formatter stringFromDate:[_datePick date]];
        dateCurrent = [_datePick date];
    }
    
    if ([self.delegate respondsToSelector:@selector(clickBtn:dateTime:)]) {
        [self.delegate clickBtn:btn.tag dateTime:dateStr];
        [self removeView];
    } else {
//        NSLog(@"协议的方法没有实现");
    }
    
}

-(void)makeView {
    _datePick = [[UIDatePicker alloc] init];
    _datePick.frame = CGRectMake(0,self.frame.size.height+40, SCREEN_WIDTH, 200);
//    if (self.isNowDate) {
//        _datePick.minimumDate = [NSDate date];
//    }
    _datePick.maximumDate = [NSDate date];
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self->_datePick.frame = CGRectMake(0,self.frame.size.height-200, SCREEN_WIDTH, 200);
    } completion:^(BOOL finished) {
        
    }];
    _datePick.backgroundColor = [UIColor whiteColor];
    _datePick.datePickerMode = UIDatePickerModeDate;
    NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
    _datePick.locale = locale;
    [_datePick addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_datePick];
}
-(void)datePickerValueChanged:(UIDatePicker *)pick {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    dateStr = [formatter stringFromDate:[pick date]];
    dateCurrent = [pick date];
    
   
}

@end
